package com.wangyk.rabbitmqcommon.fdfs;

import org.csource.fastdfs.*;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLConnection;

public class FastDFSClient implements IFileClient
{
    private TrackerClient trackerClient = null;

    private TrackerServer trackerServer = null;

    private StorageServer storageServer = null;

    private String group;

    private String tracker_server;

    // 使用StorageClient1进行上传
    private StorageClient1 storageClient1 = null;

    public String getTracker_server()
    {
        return tracker_server;
    }

    public void setTracker_server(String tracker_server)
    {
        this.tracker_server = tracker_server;
    }

    public FastDFSClient()
    {
        try
        {
            // String conf = "fdfs_client.conf";
            // 获取classpath路径下配置文件"fdfs_client.conf"的路径
            // conf直接写相对于classpath的位置，不需要写classpath:
            // URL url = this.getClass().getClassLoader().getResource(conf);
            // URL url = FastDFSClient.class.getClassLoader().getResource(conf);
            // String configPath = URLDecoder.decode(url.getFile(), "UTF-8");
            URL fileURL = this.getClass().getResource("/BOOT-INF/classes/fdfs_client.conf");
            // //System.out.println(Class.class.getClass().getResource("/").getPath());
            // String filepath = Class.class.getClass().getResource("/").getPath() + "fdfs_client.conf";
            // //System.out.println(this.getClass().getClassLoader());
            // //System.out.println(configPath);
            ClientGlobal.init(fileURL.getFile());

            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

    }

    public FastDFSClient(String group, String tracker_server)
    {
        this.group = group;
        this.tracker_server = tracker_server;
        try
        {

            ClientGlobal.setG_anti_steal_token(false);
            ClientGlobal.setG_charset("UTF-8");
            ClientGlobal.setG_connect_timeout(120);
            ClientGlobal.setG_network_timeout(120);
            ClientGlobal.setG_secret_key("FastDFS1234567890");

            ClientGlobal.setG_tracker_http_port(8080);
            InetSocketAddress i = new InetSocketAddress(tracker_server, 22122);
            InetSocketAddress[] tracker_servers = new InetSocketAddress[] {i};

            TrackerGroup tracker_group = new TrackerGroup(tracker_servers);
            ClientGlobal.setG_tracker_group(tracker_group);

            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageServer = trackerClient.getStoreStorage(trackerServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

    }

    public String uploadFile(byte[] fileBuff, String fileName)
    {

        String result;
        try
        {
            // 取扩展名，不要"."
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
            result = storageClient1.upload_file1(fileBuff, extName, null);
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

        try
        {
            Thread.sleep(1000); // 1000 毫秒，也就是1秒.
        }
        catch (InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }

        return result;
    }

    public String uploadFile(String localFilename, String fileName)
    {

        String result;
        try
        {
            // 取扩展名，不要"."
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
            result = storageClient1.upload_file1(localFilename, extName, null);
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

        return result;
    }

    public int deleteFile(String url)
    {
        int result = 0;
        try
        {
            if (null == url)
            {
                return 0;
            }

            int index = url.indexOf("/");
            String remoteFileName = url.substring(index + 1);
            String groupName = url.substring(0, index);
            result = storageClient1.delete_file(groupName, remoteFileName);
        }
        catch (Exception e)
        {
        }
        return result;
    }

    public void clientShutdown()
    {

    }

    // @Override
    // public String getFilePrefectUrl(String partOfUrl)
    // {
    // return group + partOfUrl;
    // }

    public InputStream getDownloadInput(String filePath)
    {
        // 从文件系统上取出文件流
        try
        {
            // filePath为部分路径
            URL url = new URL(group + filePath);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            return inputStream;
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

    }

}
